package com.frxuexi.visit.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.base.entity.VisitBeanItem
import com.frxuexi.base.mvi.BaseActivity
import com.frxuexi.base.mvi.RequestType
import com.frxuexi.base.mvi.UiState
import com.frxuexi.visit.R
import com.frxuexi.visit.VisitIntent
import com.frxuexi.visit.VisitViewModel
import com.frxuexi.visit.databinding.ActivityVisitAddBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/visit/VisitAddActivity")
@AndroidEntryPoint
class VisitAddActivity : BaseActivity<ActivityVisitAddBinding,VisitIntent,VisitViewModel>() {
    @Autowired
    lateinit var vid:String
    override fun getVM(): VisitViewModel {
        return viewModels<VisitViewModel>().value
    }

    override fun getBind(): ActivityVisitAddBinding {
        return ActivityVisitAddBinding.inflate(layoutInflater)
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.ADD->{
                                ToastUtils.showLong("预约成功")
                                finish()
                            }
                            RequestType.DEFAULT->{
                                val item = it.data as VisitBeanItem
                                binding.bean=item
                            }
                            RequestType.UPDATE->{
                                ToastUtils.showLong("修改成功")
                                finish()
                            }

                            else -> {}
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        if(vid.isNotEmpty()){
            vm.sendIntent(VisitIntent.getVisitById(vid.toInt()))
        }
        binding.visitaddSwitch.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.visitaddCar.isEnabled=isChecked
        }

        binding.visitaddIvDate.setOnClickListener {
            showDateDialog {
                binding.visitaddDate.setText(it)
            }
        }

        binding.visitaddIvStart.setOnClickListener {
            showTimeDialog {
                binding.visitaddStart.setText(it)
            }
        }

        binding.visitaddIvEnd.setOnClickListener {
            showTimeDialog {
                binding.visitaddEnd.setText(it)
            }
        }

        binding.visitaddSave.setOnClickListener {
            val uname = binding.visitaddName.text.toString()
            val phone = binding.visitaddPhone.text.toString()
            val carId = binding.visitaddCardId.text.toString()
            val date = binding.visitaddDate.text.toString()
            val time = binding.visitaddStart.text.toString()
            val purpose = binding.visitaddPurpose.text.toString()
            val carNo = binding.visitaddCar.text.toString()
            val people = binding.visitaddPeople.text.toString()
            val item = VisitBeanItem(
                "${date} ${time}",
                false,
                emptyList<String>(),
                people.toInt(),
                null,
                carId,
                phone,
                carNo,
                purpose,
                binding.visitaddCar.isEnabled,
                "待审核",
                uname,0
            )
            if(vid.isNotEmpty()){//修改
                item.id=vid.toInt()
                vm.sendIntent(VisitIntent.updateVisit(vid.toInt(),item))
            }else {
                vm.sendIntent(VisitIntent.addVisit(item))
            }
        }
    }

}